package com.zzyl.controller;

import cn.hutool.core.bean.BeanUtil;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.NursingLevelDto;
import com.zzyl.entity.NursingLevel;
import com.zzyl.service.NursingLevelService;
import com.zzyl.vo.NursingLevelVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

import java.util.ArrayList;

import static com.zzyl.base.ResponseResult.success;

@RestController
@Slf4j
@RequestMapping("/nursingLevel")
@Api(tags = "护理等级管理")
public class NursingLevelController {

    @Resource
    private NursingLevelService nursingLevelService;

    @PutMapping("/update")
    @ApiOperation("修改护理等级")
    public ResponseResult update(@RequestBody NursingLevelDto dto) {
        log.info("修改护理等级: ", dto);
        nursingLevelService.update(dto);
        return success();
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation("删除护理等级")
    public ResponseResult delete(@PathVariable Long id) {
        log.info("删除护理等级: ", id);
        nursingLevelService.delete(id);
        return success();
    }

    @PutMapping("/{id}/status/{status}")
    @ApiOperation("启用/禁用护理等级")
    public ResponseResult changeStatus(NursingLevelDto dto) {
        nursingLevelService.changeStatus(dto);
        return success();
    }

    //查询所有护理等级
    @GetMapping("listAll")
    @ApiOperation("护理等级全部查询")
    public ResponseResult<NursingLevelVo> listAll() {

        ArrayList<NursingLevelVo> nursingLevelVo = nursingLevelService.listAll();

        return ResponseResult.success(nursingLevelVo);
    }

    //新增护理等级
    @PostMapping("/insert")
    @ApiOperation("护理等级新增")
    public ResponseResult add(
            @ApiParam(value = "护理计划信息", required = true)
            @RequestBody NursingLevelDto nursingLevelDto

    ) {
        NursingLevel nl = BeanUtil.toBean(nursingLevelDto, NursingLevel.class);
        nursingLevelService.insert(nl);
        return ResponseResult.success();
    }

    //分页查询
    @GetMapping("/listByPage")
    @ApiOperation("护理等级分页查询")
    public ResponseResult<PageResponse<NursingLevelVo>> listByPage(
            @ApiParam(value = "名称", required = false) String name,
            @ApiParam(value = "状态值，0:禁用，1:启用", required = true) Integer status,
            @ApiParam(value = "当前页码", required = true) Integer pageNum,
            @ApiParam(value = "每页大小", required = true) Integer pageSize) {
        PageResponse<NursingLevelVo> np = nursingLevelService.listByPage(name, status, pageNum, pageSize);
        return ResponseResult.success(np);
    }

    //根据id查询
    @GetMapping("/{id}")
    @ApiOperation("护理等级根据id查询")
    public ResponseResult<NursingLevelVo> getById(
            @ApiParam(value = "护理计划id", required = true)
            @PathVariable("id") Long id){
        NursingLevelVo nl = nursingLevelService.getById(id);
        return ResponseResult.success(nl);
    }
}